package com.cloud.bc.controller.pc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.config.Config;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.enums.CookieConst;
import com.cloud.bc.model.request.SingleListRequest;
import com.cloud.bc.model.response.SingleSearchResponse;
import com.cloud.bc.model.response.UserGiftCountResponse;
import com.cloud.bc.service.*;
import com.cloud.bc.util.JiaoYouPageHelper;
import com.cloud.common.util.CookieUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

//PC单身
@Controller
@AllArgsConstructor
@RequestMapping("single")
public class PcSingleController extends BaseController {
    private final BcSinglesService bcSinglesService;
    private final BcSingleMatesService bcSingleMatesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcArticlesService bcArticlesService;
    private final BcUsersService bcUsersService;
    private final BcCategorysService bcCategorysService;
    private final BcCategoryDistrictsService bcCategoryDistrictsService;
    private final BcUserGiftsService bcUserGiftsService;
    private final Config config;

    /**
     * 寻觅
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response,
                              SingleListRequest req
    ) {
        ModelAndView view = baseView(request, "51找朋友", "pc/single/index");
        view.addObject("bodyCss", "page-single-index");
        Page page = new Page();
        page.setCurrent(Convert.toInt(req.getPage(), 1));
        page.setSize(15);
        List<SingleSearchResponse> listSingle = bcSinglesService.selectSingleSearch(page, req, request);
        JiaoYouPageHelper jiaoYouPageHelper = new JiaoYouPageHelper();
        view.addObject("page", jiaoYouPageHelper.getBcPageHTML(page, request));
        view.addObject("data", listSingle);
        view.addObject("searchModel", req);
        //学历
        view.addObject("baseEducation", bcCategorysService.getCategoryList("BC_education"));
        //收入
        view.addObject("baseIncome", bcCategorysService.getCategoryList("BC_income"));
        //婚姻状态
        view.addObject("baseMarriage", bcCategorysService.getCategoryList("BC_marriage"));
        //孩子
        view.addObject("baseChildren", bcCategorysService.getCategoryList("BC_children"));
        //结婚计划
        view.addObject("baseMarry", bcCategorysService.getCategoryList("BC_marry"));
        //民族
        view.addObject("baseNationality", bcCategorysService.getCategoryList("BC_nationality"));
        //从事行业
        view.addObject("baseIndustry", bcCategorysService.getCategoryList("BC_industry"));
        //购房情况
        view.addObject("baseHouse", bcCategorysService.getCategoryList("BC_house"));
        //购车情况
        view.addObject("baseCar", bcCategorysService.getCategoryList("BC_car"));
        //家庭背景
        view.addObject("baseFamilybackg", bcCategorysService.getCategoryList("BC_family_backg"));
        //宗教信仰
        view.addObject("baseReligious", bcCategorysService.getCategoryList("BC_religious"));

        List<BcCategoryDistricts> allCategoryDistrictsCache = bcCategoryDistrictsService.getAllCategoryDistrictsCache();
        List<BcCategoryDistricts> listProvince = allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(0)).collect(Collectors.toList());
        view.addObject("listProvince", listProvince);

        //所在区域
        viewCategoryDistricts(req.getDistrict(), allCategoryDistrictsCache, "distrcit", view, false);
        //籍贯
        viewCategoryDistricts(req.getNativePlace(), allCategoryDistrictsCache, "nativeplace", view, true);

        return view;
    }

    //区域
    private void viewCategoryDistricts(String district,
                                       List<BcCategoryDistricts> allCategoryDistrictsCache,
                                       String viewType,
                                       ModelAndView view,
                                       boolean isNativePlace) {
        //显示省市区
        if (StrUtil.isEmpty(district) || district.equals("..") || district.equals(".")) {
            return;
        }
        String[] districtArr = district.split("\\.");
        Integer provinceId = districtArr.length >= 1 ? Convert.toInt(districtArr[0], 0) : 0;
        Integer cityId = districtArr.length >= 2 ? Convert.toInt(districtArr[1], 0) : 0;
        Integer areaId = districtArr.length >= 3 ? Convert.toInt(districtArr[2], 0) : 0;
        if (provinceId > 0) {
            List<BcCategoryDistricts> listCity = allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(provinceId)).collect(Collectors.toList());
            view.addObject(viewType + "ProvinceId", provinceId);
            view.addObject(viewType + "ListCity", listCity);
        }
        if (cityId > 0) {
            view.addObject(viewType + "CityId", cityId);
            if (!isNativePlace) {
                List<BcCategoryDistricts> listArea = allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(cityId)).collect(Collectors.toList());
                view.addObject(viewType + "ListArea", listArea);
                view.addObject(viewType + "AreaId", areaId);
            }
        }
    }

    /**
     * 单身详情
     */
    @GetMapping("info/{id}")
    public ModelAndView info(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("id") Integer id) {
        //单身信息
        BcSingles bcSingles = bcSinglesService.selectOne(new BcSingles().setUserId(id));
        if (null == bcSingles || Convert.toInt(bcSingles.getUserId(), 0) <= 0) {
            return new ModelAndView("redirect:/");
        }
        bcSingles.setAge(bcSinglesService.ageConvert(bcSingles.getBirthday()));
        ModelAndView view = baseView(request, "单身信息-" + bcSingles.getNickname(), "pc/single/info");
        //对方信息
        BcSingleMates bcSingleMates = bcSingleMatesService.selectOne(new BcSingleMates().setUserId(id));
        //用户相册
        List<BcUserPhotos> bcUserPhotos = bcUserPhotosService.selectAll(new BcUserPhotos().setUserId(id));
        //收到的礼物
        List<UserGiftCountResponse> gift = bcUserGiftsService.getReceiveGiftCount(id);
        //用户
        BcUsers user = bcUsersService.getById(id);

        //脱单宝典
        IPage<BcArticles> bcArticlesIPage = bcArticlesService.selectPage(new Page(1, 10), null);
        view.addObject("article", bcArticlesIPage.getRecords());
        view.addObject("single", bcSingles);
        view.addObject("baseInfo", baseInfo(bcSingles));
        view.addObject("baseMateInfo", baseMateInfo(bcSingleMates));
        view.addObject("detailInfo", detailInfo(bcSingles));
        view.addObject("detailMateInfo", detailMateInfo(bcSingleMates));
        view.addObject("singlePhoto", bcUserPhotos);
        view.addObject("singlePhotoCount", bcUserPhotos.size());

        view.addObject("userPhone", Convert.toBigInteger(user.getMobile(), BigInteger.ZERO));
        view.addObject("userEmail", user.getMobile());
        view.addObject("userWechat", user.getOpenid());
        view.addObject("gift", gift);

        UserInfo userInfo = bcUsersService.getUserInfo(request);
        view.addObject("isVip", bcSinglesService.isVip(userInfo));
        //可能喜欢
        List<SingleSearchResponse> mayLikeList = bcSinglesService.mayLike(userInfo, request);
        view.addObject("mayLike", CollectionUtil.split(mayLikeList, 10));
//        view.addObject("isInfo", 1);
        view.addObject("userId", id);
        view.addObject("bodyCss", "page_single_show");
        return view;
    }

    private String baseInfo(BcSingles bcSingles) {
        StringBuilder sb = new StringBuilder();
        int gender = Convert.toInt(bcSingles.getGender(), 0);
        if (gender == 1) {
            sb.append("<li><span>性别：</span><span>男</span></li>");
        } else if (gender == 2) {
            sb.append("<li><span>性别：</span><span>女</span></li>");
        }
        Integer height = Convert.toInt(bcSingles.getHeight(), 0);
        if (height > 0) {
            sb.append("<li><span>身高：</span><span>" + height + "cm</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getDistrictCn())) {
            sb.append("<li><span>所在区域：</span><span>" + bcSingles.getDistrictCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getIncomeCn())) {
            sb.append("<li><span>收入：</span><span>" + bcSingles.getIncomeCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getEducationCn())) {
            sb.append("<li><span>学历：</span><span>" + bcSingles.getEducationCn() + "</span></li>");
        }

        if (StrUtil.isNotEmpty(bcSingles.getConstellationCn())) {
            sb.append("<li><span>星座：</span><span>" + bcSingles.getConstellationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getNativePlaceCn())) {
            sb.append("<li><span>籍贯：</span><span>" + bcSingles.getNativePlaceCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getReligiousCn())) {
            sb.append("<li><span>宗教信仰：</span><span>" + bcSingles.getReligiousCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getHobbyCn())) {
            sb.append("<li><span>爱好：</span><span>" + bcSingles.getHobbyCn() + "</span></li>");
        }
        return sb.toString();
    }

    private String baseMateInfo(BcSingleMates bcSingleMates) {
        if (null == bcSingleMates) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int gender = Convert.toInt(bcSingleMates.getMateGender(), 0);
        if (gender == 1) {
            sb.append("<li><span>对方性别：</span><span>男</span></li>");
        } else if (gender == 2) {
            sb.append("<li><span>对方性别：</span><span>女</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateAge()))) {
            sb.append("<li><span>对方年龄：</span><span>" + bcSingleMates.getMateAge() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateHeight()))) {
            sb.append("<li><span>对方身高：</span><span>" + bcSingleMates.getMateHeight() + "cm</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateWeight()))) {
            sb.append("<li><span>对方体重：</span><span>" + bcSingleMates.getMateWeight() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateIncomeCn()))) {
            sb.append("<li><span>对方收入：</span><span>" + bcSingleMates.getMateIncomeCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateEducationCn()))) {
            sb.append("<li><span>对方学历：</span><span>" + bcSingleMates.getMateEducationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateDistrictCn()))) {
            sb.append("<li><span>对方所在区域：</span><span>" + bcSingleMates.getMateDistrictCn() + "</span></li>");
        }

        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateConstellationCn()))) {
            sb.append("<li><span>对方星座：</span><span>" + bcSingleMates.getMateConstellationCn() + "</span></li>");
        }

        return sb.toString();
    }

    private String detailInfo(BcSingles bcSingles) {
        StringBuilder sb = new StringBuilder();
        int gender = Convert.toInt(bcSingles.getGender(), 0);
        if (gender == 1) {
            sb.append("<li><span>性别：</span><span>男</span></li>");
        } else if (gender == 2) {
            sb.append("<li><span>性别：</span><span>女</span></li>");
        }
        Integer height = Convert.toInt(bcSingles.getHeight(), 0);
        if (height > 0) {
            sb.append("<li><span>身高：</span><span>" + height + "cm</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getDistrictCn())) {
            sb.append("<li><span>所在区域：</span><span>" + bcSingles.getDistrictCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getBloodCn())) {
            sb.append("<li><span>血型：</span><span>" + bcSingles.getBloodCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getIncomeCn())) {
            sb.append("<li><span>收入：</span><span>" + bcSingles.getIncomeCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getEducationCn())) {
            sb.append("<li><span>学历：</span><span>" + bcSingles.getEducationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getMarriageCn())) {
            sb.append("<li><span>婚姻状态：</span><span>" + bcSingles.getMarriageCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getChildrenCn())) {
            sb.append("<li><span>孩子情况：</span><span>" + bcSingles.getChildrenCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getConstellationCn())) {
            sb.append("<li><span>星座：</span><span>" + bcSingles.getConstellationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getZodiacCn())) {
            sb.append("<li><span>生肖：</span><span>" + bcSingles.getZodiacCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getMarriageCn())) {
            sb.append("<li><span>结婚计划：</span><span>" + bcSingles.getMarriageCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getNationalityCn())) {
            sb.append("<li><span>民族：</span><span>" + bcSingles.getNationalityCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getDispositionCn())) {
            sb.append("<li><span>性格：</span><span>" + bcSingles.getDispositionCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getHouseCn())) {
            sb.append("<li><span>房产：</span><span>" + bcSingles.getHouseCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getCarCn())) {
            sb.append("<li><span>汽车：</span><span>" + bcSingles.getCarCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getIndustryCn())) {
            sb.append("<li><span>行业：</span><span>" + bcSingles.getIndustryCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getProfessionCn())) {
            sb.append("<li><span>职业：</span><span>" + bcSingles.getProfessionCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getNativePlaceCn())) {
            sb.append("<li><span>籍贯：</span><span>" + bcSingles.getNativePlaceCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getFamilyBackgCn())) {
            sb.append("<li><span>家庭背景：</span><span>" + bcSingles.getFamilyBackgCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getDrinkCn())) {
            sb.append("<li><span>喝酒：</span><span>" + bcSingles.getDrinkCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getSmokingCn())) {
            sb.append("<li><span>吸烟：</span><span>" + bcSingles.getSmokingCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getWorkOutCn())) {
            sb.append("<li><span>健身习惯：</span><span>" + bcSingles.getWorkOutCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getWorkRestCn())) {
            sb.append("<li><span>作息习惯：</span><span>" + bcSingles.getWorkRestCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getPetCn())) {
            sb.append("<li><span>宠物：</span><span>" + bcSingles.getPetCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getReligiousCn())) {
            sb.append("<li><span>宗教信仰：</span><span>" + bcSingles.getReligiousCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(bcSingles.getHobbyCn())) {
            sb.append("<li><span>爱好：</span><span>" + bcSingles.getHobbyCn() + "</span></li>");
        }
        return sb.toString();
    }

    private String detailMateInfo(BcSingleMates bcSingleMates) {
        if (null == bcSingleMates) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int gender = Convert.toInt(bcSingleMates.getMateGender(), 0);
        if (gender == 1) {
            sb.append("<li><span>对方性别：</span><span>男</span></li>");
        } else if (gender == 2) {
            sb.append("<li><span>对方性别：</span><span>女</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateAge()))) {
            sb.append("<li><span>对方年龄：</span><span>" + bcSingleMates.getMateAge() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateHeight()))) {
            sb.append("<li><span>对方身高：</span><span>" + bcSingleMates.getMateHeight() + "cm</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateWeight()))) {
            sb.append("<li><span>对方体重：</span><span>" + bcSingleMates.getMateWeight() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateIncomeCn()))) {
            sb.append("<li><span>对方收入：</span><span>" + bcSingleMates.getMateIncomeCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateEducationCn()))) {
            sb.append("<li><span>对方学历：</span><span>" + bcSingleMates.getMateEducationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateDistrictCn()))) {
            sb.append("<li><span>对方所在区域：</span><span>" + bcSingleMates.getMateDistrictCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateMarriageCn()))) {
            sb.append("<li><span>对方婚姻状态：</span><span>" + bcSingleMates.getMateMarriageCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateChildrenCn()))) {
            sb.append("<li><span>对方孩子情况：</span><span>" + bcSingleMates.getMateChildrenCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateConstellationCn()))) {
            sb.append("<li><span>对方星座：</span><span>" + bcSingleMates.getMateConstellationCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateMarriageCn()))) {
            sb.append("<li><span>对方结婚计划：</span><span>" + bcSingleMates.getMateMarriageCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateNationalityCn()))) {
            sb.append("<li><span>对方民族：</span><span>" + bcSingleMates.getMateNationalityCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateHouseCn()))) {
            sb.append("<li><span>对方房产：</span><span>" + bcSingleMates.getMateHouseCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateCarCn()))) {
            sb.append("<li><span>对方汽车：</span><span>" + bcSingleMates.getMateCarCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateIndustryCn()))) {
            sb.append("<li><span>对方行业：</span><span>" + bcSingleMates.getMateIndustryCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateProfessionCn()))) {
            sb.append("<li><span>对方职业：</span><span>" + bcSingleMates.getMateProfessionCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateDispositionCn()))) {
            sb.append("<li><span>对方性格：</span><span>" + bcSingleMates.getMateDispositionCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateNativePlaceCn()))) {
            sb.append("<li><span>对方籍贯：</span><span>" + bcSingleMates.getMateNativePlaceCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateFamilyBackgCn()))) {
            sb.append("<li><span>对方家庭背景：</span><span>" + bcSingleMates.getMateFamilyBackgCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateDrinkCn()))) {
            sb.append("<li><span>对方喝酒：</span><span>" + bcSingleMates.getMateDrinkCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateSmokingCn()))) {
            sb.append("<li><span>对方吸烟：</span><span>" + bcSingleMates.getMateSmokingCn() + "</span></li>");
        }
        if (StrUtil.isNotEmpty(getMate(bcSingleMates.getMateReligiousCn()))) {
            sb.append("<li><span>对方宗教：</span><span>" + bcSingleMates.getMateReligiousCn() + "</span></li>");
        }
        return sb.toString();
    }

    private String getMate(String data) {
        if (StrUtil.isEmpty(data)) {
            return "";
        }
        if (data.equals("不限")) {
            return "";
        }
        return data;
    }

}
